Skip to content

fix(db): prevent duplicate member segment affiliations#3985

Open
prembharne wants to merge 4 commits intolinuxfoundation:mainfrom
prembharne:main
Open

fix(db): prevent duplicate member segment affiliations#3985
prembharne wants to merge 4 commits intolinuxfoundation:mainfrom
prembharne:main

Conversation

@prembharne
Copy link
Copy Markdown

@prembharne prembharne commented Mar 31, 2026

Fixes #2840

Description:
This PR introduces a strict deduplication migration for the memberSegmentAffiliations table and enforces a UNIQUE index that handles NULL date values natively using COALESCE.

Additionally, we added a robust pre-insert SELECT check in memberSegmentAffiliationRepository.ts's createOrUpdate function. This prevents unhandled 500 Constraint Errors and updates seamlessly when exact matches are triggered, ensuring consistency across segments.

Changes:

Added .sql cleanup and indexing migration.
Graceful TypeScript fail-safes in backend integration preventing blind inserts.


Note

Medium Risk
Adds a data-cleanup migration and a new uniqueness constraint on memberSegmentAffiliations, which could impact production data and fail if unexpected duplicates/NULL patterns exist. Also changes insert behavior to upsert on conflicts, affecting write semantics for this table.

Overview
Cleans up the memberSegmentAffiliations table by deleting exact duplicate rows (same member/segment/org/dateStart/dateEnd) and then enforces a new unique index that treats NULL values as equal via COALESCE.

Updates MemberSegmentAffiliationRepository.createOrUpdate to use INSERT ... ON CONFLICT against that same composite key (including the COALESCE expressions), turning duplicate inserts into an upsert instead of raising a constraint error.

Written by Cursor Bugbot for commit 78f029e. This will update automatically on new commits. Configure here.

Adds a pre-insert SELECT check in TypeScript and a concrete UNIQUE index migration on memberSegmentAffiliations robustly resolving duplicate affiliation logging problems.

Signed-off-by: Prem bharne <prembharne455@gmail.com>
Signed-off-by: Prem bharne <prembharne455@gmail.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 31, 2026

CLA assistant check
All committers have signed the CLA.

Eliminates race condition under READ COMMITTED isolation and fixes COALESCE sentinel mismatch between the unique index and the application-level query.

Signed-off-by: Prem bharne <prembharne455@gmail.com>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

membersegmentaffiliations allows duplicated affiliations

2 participants